import http from '@/utils/http'
import { encryption } from '@/utils/util'
import { encryptionKey } from '@/settings'
import { LOGIN_TYPE } from '../configuration'

export const verifyMixin = {
  created: function () {},
  methods: {
    /**
     * @description 发送验证码
     */
    sendVerification() {
      const user = encryption({
        data: this.form,
        key: encryptionKey,
        // todo 扫码登录此处unionid也加密了，但是不知道需不需要？？？
        param: ['password', 'username']
      })

      const { username, password } = user

      const param = {
        loginType: LOGIN_TYPE[this.tabKey],
        loginTab: this.loginTab,
        verifyCodeType: this.verificationCodeType,
        captchaCode: this.form.code,
        randomStr: this.form.randomStr,
        domain: this.domain,
        appCode: this.appCode
      }

      if (password) {
        param['password'] = password
      }

      if (username) {
        param['account'] = username
      }

      http.sendMsgVerify(param).then(({ code, data }) => {
        if (code === '200') {
          console.log('验证码发送成功！')
          this.verifyCodeId = data
          this.rewrite(this.verifyCodeId)
          this.countdown = true
          this.startAwait()
        } else {
          this.refreshCode()
          console.warn('验证码获取失败!')
        }
      })
    }
  }
}
